<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

	<!--<%&#45;&#45;  bootctrap_fileinput  &#45;&#45;%>-->
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.js"></script>
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
	<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.css"/>
    
</head>

<script>

	$(function (){
		$("#categoryId").select2()
		findallContent()
		$('#myFile').fileinput({
			language: 'zh',//汉化
			uploadUrl:"/uploadController/upload",//上传地址
			showCaption: true,//是否展示显示框
			showUpload: true,//默认为true, false不显示上传按钮
			showRemove: true,//默认为true,false不显示移除按钮
			showClose: true,//默认为true, false不显示图片右上角的关闭按钮
			browseClass: 'btn btn-warning',//按钮变色
			//showPreview: true,
			//showUploadedThumbs: true
			//showBrowse:["txt",""],
			//autoReplace:true,
			allowedFileExtensions:['jpg','png','gif','txt','pdf','xls','xlsx'], //限制文件上传类型
			maxFileSize:2048,//设置文件上传大小 单位kb
			maxFileCount:5,//最大上传数量
			enctype: "multipart/form-data",//改为二进制
			// initialPreviewFileType:"image",
			// initialPreviewShowDelete:true,
			// initialPreview:"/file/download?myFile=${house.himg}",
			// initialPreviewAsData:true
		});
		//上传成功回调
		$('#myFile').on('fileuploaded',function(event, data, previewId, index){
			if(data.response != null){
				var aa = []
				$("[name=imgurl]").val(data.response.t);//abc.jpg  + "," + bcd.jpg
				aa.push(data.response.t)
				imgurl.push(aa)
			}
		})
	})

	//回显
	function echo(id){
		$("#categoryId2").select2()
		findallContent()
		$('#myFile2').fileinput({
			language: 'zh',//汉化
			uploadUrl:"/uploadController/upload",//上传地址
			showCaption: true,//是否展示显示框
			showUpload: true,//默认为true, false不显示上传按钮
			showRemove: true,//默认为true,false不显示移除按钮
			showClose: true,//默认为true, false不显示图片右上角的关闭按钮
			browseClass: 'btn btn-warning',//按钮变色
			//showPreview: true,
			//showUploadedThumbs: true
			//showBrowse:["txt",""],
			//autoReplace:true,
			allowedFileExtensions:['jpg','png','gif','txt','pdf','xls','xlsx'], //限制文件上传类型
			maxFileSize:2048,//设置文件上传大小 单位kb
			maxFileCount:5,//最大上传数量
			enctype: "multipart/form-data",//改为二进制
			// initialPreviewFileType:"image",
			// initialPreviewShowDelete:true,
			// initialPreview:$("#imgurl2").val(),
			// initialPreviewAsData:true
		});
		//上传成功回调
		$('#myFile2').on('fileuploaded',function(event, data, previewId, index){
			if(data.response != null){
				var aa = []
				$("#imgurl2").val(data.response.t);//abc.jpg  + "," + bcd.jpg
				aa.push(data.response.t)
				imgurl.push(aa)
			}
		})

		$.ajax({
			url: "/contentController/selectContentById?id=" + id,
			type: "post",
			dataType: "json",
			async: true,
			success: function (list){
				console.log(list);
				var content = list.t;
				$("#categoryId2").val(content.categoryId)
				$("#title2").val(content.title)
				$("#url2").val(content.url)
				$("#sortOrder2").val(content.sortOrder)
				$("#imgurl2").val(content.pic)
				$("#contentid").val(content.id)
				if (content.status == 1){
					$("#status2").prop("checked",true)
				}else {
					$("#status2").prop("checked",false)
				}
			},
			error: function (list){
				console.log(list);
				alert("网络错误")
			}
		})
	}

	//拼接页面
	function findallContent(){
		$.ajax({
			url: "/contentController/findallContent",
			type: "post",
			dataType: "json",
			async: true,
			success: function (list){
				console.log(list);
				var content = list.t;
				var html = ""
				for (var i = 0; i < content.length; i++) {
					html += '<tr><td><input type="checkbox" name="check" value="' + content[i].id + '"></td><td>' + content[i].id + '</td><td>' + content[i].categoryId + '</td>'
					html += '<td>' + content[i].title + '</td><td>' + content[i].url + '</td>'
					html += '<td><img alt="" src="' + content[i].pic + '" width="100px" height="50px"></td>'
					html += '<td>' + content[i].sortOrder + '</td>'
					if (content[i].status == 0){
						html += '<td>失效</td>'
					}
					if (content[i].status == 1){
						html += '<td>有效</td>'
					}

					html += '<td className="text-center">'
					html += '<button type="button" onclick="echo(' + content[i].id + ')" className="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal2">修改</button></td></tr>'

				}

				$("#tbodyid").html(html)

			},
			error: function (list){
				console.log(list);
				alert("网络错误")
			}
		})
	}

	//增加
	function insertContent(){
		//获取select2的value
		var select2 = $("#categoryId").select2("data");
		var brandid = {id:select2.id}
		var status = 0
		if ($("#status").prop("checked")){
			status = 1
		}
		$.ajax({
			url: "/contentController/insertContent",
			type: "post",
			dataType: "json",
			data: {categoryId:brandid.id,title:$("#title").val(),url:$("#url").val(),sortOrder:$("#sortOrder").val(),pic:$("#imgurl").val(),status:status},
			async: true,
			success: function (list){
				console.log(list);
				alert(list.codename)
				window.location.reload();
			},
			error: function (list){
				console.log(list);
				alert("网络错误")
			}
		})
	}

	//修改
	function updateContent(){
		//获取select2的value
		var select2 = $("#categoryId2").select2("data");
		var brandid = {id:select2.id}
		var status = 0
		if ($("#status2").prop("checked")){
			status = 1
		}
		$.ajax({
			url: "/contentController/updateContent",
			type: "post",
			dataType: "json",
			data: {id:$("#contentid").val(),categoryId:brandid.id,title:$("#title2").val(),url:$("#url2").val(),sortOrder:$("#sortOrder2").val(),pic:$("#imgurl2").val(),status:status},
			async: true,
			success: function (list){
				console.log(list);
				alert(list.codename)
				window.location.reload();
			},
			error: function (list){
				console.log(list);
				alert("网络错误")
			}
		})
	}

	//删除
	function deleteContent(){
		var checks = document.getElementsByName("check")
		var ids = []
		for (var i = 0; i < checks.length; i++) {
			if (checks[i].checked){
				ids.push(checks[i].value)
			}
		}

		$.ajax({
			url: "/contentController/deleteContent?ids=" + ids,
			type: "post",
			dataType: "json",
			async: true,
			success: function (list){
				console.log(list);
				alert(list.codename)
				window.location.reload();
			},
			error: function (list){
				console.log(list);
				alert("网络错误")
			}
		})
	}
</script>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                <input type="hidden" id="imgid">
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="deleteContent()" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbodyid">

			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" id="categoryId">
							<option th:value="''">请选择</option>
							<th:block th:each="l:${list}">
								<option th:value="${l.id}" th:id="${l.id}">[[${l.name}]]</option>
							</th:block>
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" id="title">  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" id="url">  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" id="sortOrder">  </td>
		      	</tr>			      	
		      	<tr>
					<td>广告图片</td>
					<td>
						<input type="file" class="form-control" id="myFile" placeholder="请输入路线名称" name="image">
						<input type="text" name="imgurl" id="imgurl">
					</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" class="icheckbox_square-blue" id="status">
		      		</td>
		      	</tr>  	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button onclick="insertContent()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
<div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel2">广告编辑</h3>
		</div>
		<div class="modal-body">

			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
						<input type="hidden" id="contentid">
		      			<select class="form-control" id="categoryId2">
							<option th:value="''">请选择</option>
							<th:block th:each="l:${list}">
								<option th:value="${l.id}" th:id="${l.id}">[[${l.name}]]</option>
							</th:block>
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" id="title2">  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" id="url2">  </td>
		      	</tr>
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" id="sortOrder2">  </td>
		      	</tr>
		      	<tr>
					<td>广告图片</td>
					<td>
						<input type="file" class="form-control" id="myFile2" placeholder="请输入路线名称" name="image">
						<input type="text" name="imgurl2" id="imgurl2">
					</td>
		      	</tr>
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" class="icheckbox_square-blue" id="status2">
		      		</td>
		      	</tr>
			 </table>

		</div>
		<div class="modal-footer">
			<button onclick="updateContent()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

</body>

</html>